import { createWebHistory, createRouter } from 'vue-router'
/* Layout */
import Layout from '@/layout'
import {toast} from "@/utils/common.js";
import {getToken} from "@/utils/auth.js";


// 公共路由
export const constantRoutes = [
  {
    path: '/redirect',
    component: Layout,
    hidden: true,
    children: [
      {
        path: '/redirect/:path(.*)',
        component: () => import('@/views/redirect/index.vue')
      }
    ]
  },
  {
    path: '/login',
    component: () => import('@/views/login'),
    hidden: true
  },
  {
    path: '/register',
    component: () => import('@/views/register'),
    hidden: true
  },
  {
    path: "/:pathMatch(.*)*",
    component: () => import('@/views/error/404'),
    hidden: true
  },
  {
    path: '/401',
    component: () => import('@/views/error/401'),
    hidden: true
  },
  {
    path: '',
    component: Layout,
    redirect: '/index',
    children: [
      {
        path: '/index',
        component: () => import('@/views/index'),
        name: 'Index',
        meta: { title: '首页', icon: 'dashboard', affix: true }
      }
    ]
  },
  {
    path: '/publish',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'index',
        component: () => import('@/views/wallpaper/publish/index'),
        name: 'Publish',
      },
    ]
  },
  {
    path: '/user',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'profile',
        component: () => import('@/views/system/user/profile/index'),
        name: 'Profile',
        meta: { title: '个人中心', icon: 'user' }
      }
    ]
  },
  {
    path: '/classify',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'index',
        component: () => import('@/views/wallpaper/classify/index'),
        name: 'Classify',
        meta: { title: '分类'}
      }
    ]
  },
  {
    path: '/classlist',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'index',
        component: () => import('@/views/wallpaper/classlist/index'),
        name: 'ClassList',
        // 使用 props 将查询参数传递给组件
        props: route => ({
          id: route.query.id,
          type: route.query.type
        }),
        meta: { title: '分类列表' }
      }
    ]
  },
  {
    path: '/preview',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'wallpaper/:id',
        component: () => import('@/views/wallpaper/previewWallpaper/index'),
        name: 'PreviewWallpaper',
        meta: { title: '预览壁纸'}
      },
      {
        path: 'album/:id',
        component: () => import('@/views/wallpaper/previewAlbum/index'),
        name: 'PreviewAlbum',
        meta: { title: '预览相册'}
      },
    ]
  },
  {
    path: '/rank',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'index',
        component: () => import('@/views/wallpaper/rank/index'),
        name: 'Rank',
        meta: { title: '榜单'}
      }
    ]
  },
  {
    path: '/search',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: 'index',
        component: () => import('@/views/wallpaper/search/index'),
        name: 'Search',
        meta: { title: '搜索'}
      }
    ]
  },
  {
    path: '/notice',
    component: Layout,
    hidden: true,
    redirect: 'noredirect',
    children: [
      {
        path: ':id',
        component: () => import('@/views/system/notice/index'),
        name: 'Notice',
        meta: { title: '公告' }
      }
    ]
  }
]


const router = createRouter({
  history: createWebHistory(),
  routes: constantRoutes,
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition
    } else {
      return { top: 0 }
    }
  },
});

router.beforeEach((to, from, next) => {
  // 检查用户是否已登录
  const isAuthenticated = !!getToken();

  if (to.name === 'Publish' || to.name === 'Profile') {
    if (!isAuthenticated) {
      toast('请先登录');
      // 如果用户未登录，重定向到登录页面
      next({ path: '/login' });
    } else {
      // 如果用户已登录，允许访问
      next();
    }
  } else {
    // 对于其他路由，直接放行
    next();
  }
});

export default router;
